package com.community.web.master.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.model.entity.Area;
import com.community.model.entity.Goods;
import com.community.model.entity.GoodsCategory;
import com.community.web.master.domain.dto.GoodsCategoryDto;
import com.community.web.master.domain.dto.PageGoodsCategoryDto;
import com.community.web.master.domain.vo.PageResult;
import com.community.web.master.exception.DataException;
import com.community.web.master.exception.DeletionNotAllowedException;
import com.community.web.master.mapper.GoodsCategoryMapper;
import com.community.web.master.mapper.GoodsMapper;
import com.community.web.master.service.IGoodsCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李晶
 * @since 2024-07-02
 */
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory> implements IGoodsCategoryService {

    @Autowired
    GoodsCategoryMapper goodsCategoryMapper;
    @Autowired
    GoodsMapper goodsMapper;
    /**
     * 分页查询
     * @param pageGoodsCategoryDto
     * @return
     */
    @Override
    public PageResult pageQuery(PageGoodsCategoryDto pageGoodsCategoryDto) {
        IPage<GoodsCategory> page= new Page<GoodsCategory>(pageGoodsCategoryDto.getPage(),pageGoodsCategoryDto.getPageSize());

        QueryWrapper<GoodsCategory> wrapper = new QueryWrapper<GoodsCategory>();
        if(pageGoodsCategoryDto.getCategoryName()!=null){
            wrapper.like("category_name",pageGoodsCategoryDto.getCategoryName());}
        goodsCategoryMapper.selectPage(page,wrapper);
        return new PageResult(page.getTotal(),page.getRecords().stream()
                .map(res -> {
                    if (res.getParentId() == null) {
                        res.setParentId(0);
                    }
                    return res;
                })
                .collect(Collectors.toList()));
    }

    /**
     * 增加分类
     * @param parentId
     * @param categoryName
     */
    @Override
    public void add(Integer parentId, String categoryName) {
        /**
         * 查询父类型
         */
        GoodsCategory parent = goodsCategoryMapper.selectById(parentId);
        if(parent.getCategoryType()==3){
            throw new DataException("上级分类不允许是三级分类");
        }
        GoodsCategory goodsCategory = new GoodsCategory();
        if(parentId==null||parentId==0){
            goodsCategory.setParentId(0);
        }else {
            if(goodsCategoryMapper.selectOne(
                    new QueryWrapper<GoodsCategory>().eq("parent_id",parentId))
            ==null) throw new DataException("父类别不存在");
            goodsCategory.setParentId(parentId);
        }

        goodsCategory.setCategoryName(categoryName);
        goodsCategory.setCategoryType(parent.getCategoryType()+1);
        goodsCategoryMapper.insert(goodsCategory);
    }

    @Override
    public void updateCategory(GoodsCategoryDto goodsCategoryDto) {
        //是否是同级区域?同级可以直接改
        GoodsCategory data = new GoodsCategory();
        GoodsCategory goodsCategory = goodsCategoryMapper.selectById(goodsCategoryDto.getId());
        GoodsCategory newParent = new GoodsCategory();
        //不同父类
        if(goodsCategory.getParentId()!=goodsCategoryDto.getParentId()){
            newParent = goodsCategoryMapper.selectById(goodsCategoryDto.getParentId());
            if(newParent==null){
                //要改成一级目录,如果底下没有任何类别也可以改
                QueryWrapper<GoodsCategory> wrapper = new QueryWrapper<GoodsCategory>().eq("parent_id",goodsCategoryDto.getParentId());
                if(goodsCategoryMapper.exists(wrapper)){
                    throw new DataException("有子类别,无法更改类别");
                }
                goodsCategoryMapper.updateById(data);
                return;
            }
            else if(newParent.getCategoryType()==3){
                throw new DataException("三级类别下不允许加新类别");
            }
        }
       //不同父类但是等级一样
        newParent = goodsCategoryMapper.selectById(goodsCategoryDto.getParentId());
        if(goodsCategory.getCategoryType()==newParent.getCategoryType()+1){
            BeanUtils.copyProperties(goodsCategoryDto,data);
            goodsCategoryMapper.updateById(data);
            return;
        }
        //不是同级,如果底下没有任何类别也可以改
        QueryWrapper<GoodsCategory> wrapper = new QueryWrapper<GoodsCategory>().eq("parent_id",goodsCategoryDto.getParentId());
        if(goodsCategoryMapper.exists(wrapper)){
            throw new DataException("有子类别,无法更改类别");
        }
        goodsCategoryMapper.updateById(data);
    }

    @Override
    public void deleteById(Integer id) {
        //查出要删的类别
        GoodsCategory goodsCategory = goodsCategoryMapper.selectById(id);
        if(goodsCategory == null) throw new DataException("数据有误");
        if(goodsCategory.getCategoryType()<3){
            List<GoodsCategory> child = goodsCategoryMapper.selectList(new QueryWrapper<GoodsCategory>().eq("parent_id", id));
            if (child!=null) throw new DeletionNotAllowedException("有子类别,不能删除");
        }
        List<Goods> goods = goodsMapper.selectList(new QueryWrapper<Goods>().eq("category_id", id));
        if (goods != null){
            if(goods.size()>0)  throw new DeletionNotAllowedException("类别关联商品,不能删除");
        }
        goodsCategoryMapper.deleteById(id);
    }
}
